﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GGG_Dll;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;

namespace 模板王网站资源爬取
{
    internal class Program
    {
        static GGG_IHttpHelper http = new HttpHelper();
        static string fontsHost = "https://fonts.mobanwang.com";
        static string htmlHost = "https://www.mobanwang.com";
        static Regex regex;
        static string 网页模板Path = "网页模板";
        static void Main(string[] args)
        {
            Console.WriteLine("请选择爬取的类型");
            Console.WriteLine("1、网页模板");
            Console.WriteLine("2、免费字体");
            string input = Console.ReadLine();

            switch (input)
            {
                case "1":
                    网页模板();
                    break;
                case "2":
                    免费字体();
                    break;
                default:
                    break;
            }
        }

        public static void 网页模板()
        {
            string url = $"{htmlHost}/mb";
            string html = http.GetJson(url);
            int page = 获取页数(html);
            for (int i = 2; i <= page; i++)
            {
                url = $"{htmlHost}/mb/list_{i}.html";
                html = http.GetJson(url);
                regex = new Regex("(http(.*?)rar)");
                List<string> list = regex.Matches(html).OfType<Match>().Select(x => x.Value).ToList();
                regex = new Regex("<p class=\"mb-figcaption\">(.*?)</p>");
                List<string> list1 = regex.Matches(html).OfType<Match>().Select(x => x.Groups[1].Value).ToList();

                var res = list.Zip(list1, (x, y) => (x, y));
                foreach (var item in res)
                {
                    string path = $"{网页模板Path}\\{item.y}";
                    Directory.CreateDirectory(path);
                    string save = $"{path}\\{Guid.NewGuid()}.rar";

                    http.DownLoad(item.x, "https://www.mobanwang.com/", save);
                }
            }
        }

        public static void 免费字体()
        {

        }

        public static int 获取页数(string html)
        {
            regex = new Regex("共(\\d+)个");
            int va = int.Parse(regex.Match(html).Groups[1].Value);
            return va % 30 == 0 ? va / 30 : va / 30 + 1;
        }


    }
}
